// pages/rank/rank.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: 'https://armeco.happydoit.com/upload', // 服务器图片路径
    showCityRank: false, // 展示城市排行榜
    showPersonRank: true, // 展示个人排行榜
    cityList: [], // 城市排行榜
    userRank: [], // 个人排行榜
    firstUser: null, // 可领取的用户
    winnerTicket: false, // 中门票
    winnerPicture: false, // 中签名照
    finishExchange: false, // 已经兑换过
    noWinning: false, // 未中奖弹窗
    collect: false, // 收集弹窗
    hasWrite: false, // 重复填写弹窗
    submit: false, // 提交信息弹窗
    windowHeight: "1443rpx", // 默认背景图片高度
    userName: '', // 填写的用户名
    userPhone: '', // 填写的手机号
    userIdCard: '', // 填写的身份证号
    userAddress: '', // 填写的地址,
    ticketStatus: 0, // 是否可领取门票
    signPhoto: 0, // 是否可领取签名照
    userInfo: null,
    id: '', // 查询收货信息的id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if (options.id) {
      that.setData({
        id: options.id
      })
    }
    that.getCityRank();
    that.setData({
      canFocus: app.globalData.canFocus,
      userInfo: app.globalData.userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if (that.data.id != '') {
      wx.request({
        url: app.globalData.requestUrl + '/api/city/getCityReciptInfoById',
        data: {
          reciptid: that.data.id
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
          "Cookie": "JSESSIONID=" + app.globalData.jessionId
        },
        success: function (res) {
          if (res.data.code == 'success') {
            wx.hideLoading();
            console.log(res.data.data)
            if (res.data.data.citygiftid == 4) { // 签名照
              that.setData({
                winnerPicture: true,
                userName: res.data.data.realname,
                userPhone: res.data.data.phone,
                userAddress: res.data.data.shipaddr,
                cityPrizeId: res.data.data.citygiftid
              })
            }
          } else {
            wx.hideLoading();
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              mask: true
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    return {
        title: '让邓伦来我的芯愿城市，就靠你的助力了！',
        path: '/pages/stage/cityHelp/cityHelp',
        imageUrl: that.data.imageUrl + '/cityimages/share-img.png'
      }
  },

  // 返回上一页
  back: function () {
    wx.navigateBack({
      delta: 1
    });
  },

  // 展示个人排行
  showPerson: function () {
    this.setData({
      showPersonRank: true,
      showCityRank: false
    })
  },

  showCity: function () {
    this.setData({
      showPersonRank: false,
      showCityRank: true
    })
  },

  // 获取城市排行
  getCityRank: function () {
    let that = this;
    wx.showLoading({
        title: '加载中',
        mask: true,
    });
    wx.request({
        url: app.globalData.requestUrl + '/api/city/cityRank',
        data: {},
        method: "POST",
        header: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Cookie": "JSESSIONID=" + app.globalData.jessionId
        },
        success: function (res) {
            if (res.data.code == 'success') {
                wx.hideLoading();
                that.setData({
                  cityList: res.data.data.cityRank,
                  userRank: res.data.data.userRank,
                  firstUser: res.data.data.firstUser,
                  ticketStatus: res.data.data.ticketStatus,
                  signPhoto: res.data.data.signPhoto
                })
            } else {
                wx.hideLoading();
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    mask: true
                })
            }
        }
    })
  },

  // 个人榜领取
  receivePrize: function () {
    this.setData({
      winnerTicket: true
    })
  },

  // 城市榜领取
  receivePic: function () {
    this.setData({
      winnerPicture: true
    })
  },

  // 关闭门票信息填写
  closeTicket: function () {
    this.setData({
      winnerTicket: false
    })
  },

  // 关闭签名照信息填写
  closePicture: function () {
    this.setData({
      winnerPicture: false
    })
  },

  // 关闭已经兑换过窗口
  closeFinishExchange: function () {
    this.setData({
      finishExchange: false
    })
  },

  // 关闭未中奖窗口
  closeNoWinning: function () {
    this.setData({
      noWinning: false
    })
  },

  // 关闭收集弹窗
  closeCollect: function () {
    this.setData({
      collect: false
    })
  },

  // 关闭重复填写弹窗
  closeHasWrite: function () {
    this.setData({
      hasWrite: false
    })
  },

  // 关闭提交信息弹窗
  closeSubmit: function () {
    this.setData({
      submit: false
    })
  },

  // 查看中奖记录
  myRecord: function () {
    wx.navigateTo({
      url: '/pages/redEnvelope/package',
      })
  },

  // 获取填写的姓名
  getName: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },

  // 获取填写的手机号
  getPhone: function (e) {
    this.setData({
      userPhone: e.detail.value
    })
  },

  // 获取填写的身份证号
  getIdCard: function (e) {
    this.setData({
      userIdCard: e.detail.value
    })
  },

  // 获取填写的地址
  getAddress: function (e) {
    this.setData({
      userAddress: e.detail.value
    })
  },

  // 提交中奖信息(门票)
  submitTicket: function () {
    let that = this;
    if (!that.data.userName || that.data.userName == '') {
      wx.showToast({
        title: '请输入姓名',
        mask: true,
        icon: 'none'
      })
      return;
    };
    if (!that.data.userPhone || that.data.userPhone == 0 || that.data.userPhone == '') {
      wx.showToast({
        title: '请输入手机号',
        mask: true,
        icon: 'none'
      })
      return;
    };
    if (!that.data.userIdCard || that.data.userIdCard == '' || that.data.userIdCard == 0) {
      wx.showToast({
        title: '请输入身份证号',
        mask: true,
        icon: 'none'
      })
      return;
    };
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    wx.request({
        url: app.globalData.requestUrl + '/api/city/cityRecipeInfo',
        data: {
          realname: that.data.userName,
          phone: that.data.userPhone,
          idcard: that.data.userIdCard,
          citypresentid: 1,
          reciptid: that.data.id
        },
        method: "POST",
        header: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Cookie": "JSESSIONID=" + app.globalData.jessionId
        },
        success: function (res) {
            if (res.data.code == 'success') {
                wx.hideLoading();
                wx.showLoading({
                  title: '加载中',
                  mask: true,
                })
                wx.request({
                  url: app.globalData.requestUrl + '/api/city/getCityRankTicket',
                  data: {},
                  method: "POST",
                  header: {
                      "Content-Type": "application/x-www-form-urlencoded",
                      "Cookie": "JSESSIONID=" + app.globalData.jessionId
                  },
                  success: function (e) {
                      if (e.data.code == 'success') {
                          wx.hideLoading();
                          if (that.data.ticketStatus == 1) {
                            that.setData({
                              submit: true,
                              winnerTicket: false
                            })
                            that.getCityRank();
                          }
                      } else {
                          wx.hideLoading();
                          wx.showToast({
                              title: e.data.msg,
                              icon: 'none',
                              mask: true
                          })
                      }
                  }
                })
            } else {
                wx.hideLoading();
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    mask: true
                })
            }
        }
    })
  },

  // 提交中奖信息(签名照)
  submitPic: function () {
    let that = this;
    if (!that.data.userName || that.data.userName == '') {
      wx.showToast({
        title: '请输入姓名',
        mask: true,
        icon: 'none'
      })
      return;
    };
    if (!that.data.userPhone || that.data.userPhone == 0 || that.data.userPhone == '') {
      wx.showToast({
        title: '请输入手机号',
        mask: true,
        icon: 'none'
      })
      return;
    };
    if (!that.data.userAddress || that.data.userAddress == '') {
      wx.showToast({
        title: '请输入地址',
        mask: true,
        icon: 'none'
      })
      return;
    };
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    wx.request({
        url: app.globalData.requestUrl + '/api/city/cityRecipeInfo',
        data: {
          realname: that.data.userName,
          phone: that.data.userPhone,
          shipaddr: that.data.userAddress,
          citypresentid: 4,
          reciptid: that.data.id
        },
        method: "POST",
        header: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Cookie": "JSESSIONID=" + app.globalData.jessionId
        },
        success: function (res) {
            if (res.data.code == 'success') {
                wx.hideLoading();
                that.setData({
                  submit: true,
                  winnerPicture: false
                })
                that.getCityRank();
            } else {
                wx.hideLoading();
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    mask: true
                })
            }
        }
    })
  },

  showFocus() {
    this.setData({
      isFocus: true
    })
  },

  hideFocus() {
    this.setData({
      isFocus: false
    })
  },

  openRule() {
    this.setData({
      showRule: true
    })
  },

  onGetCode: function (e) {
    this.setData({
      showRule: e.detail.val
    })
  }
})